<?php
/**
 * Created by PhpStorm.
 * User: liuhong
 * Date: 2019-04-09
 * Time: 14:07
 */

namespace app\api\controller;


use app\service\CardsService;
use app\service\CardThumbService;

class Cardthumb extends Base
{
    //点赞、取消点赞
    public function add_thumb(){
        $input = input('');
        if(!isset($input['card_id']) || empty($input['card_id'])){
            $this->ajax(202,'参数不完整');
        }else{
            $card_info = CardsService::S()->findCard(['a.id'=>['eq',$input['card_id']]]);
            if(empty($card_info)){
                $this->ajax(202,'名片信息不存在');
            }else if($card_info['user_id']== $this->uid){
                $this->ajax(202,'不能点赞自己的名片');
            }
        }

        $save_data = [
            'card_id' => $input['card_id'],
            'thumb_id' => $this->uid,
            'thumb_time' => time()
        ];
        $where = [
            'card_id' => ['eq',$input['card_id']],
            'thumb_id' => ['eq',$this->uid]
        ];
        $user_info = CardThumbService::S()->findThumb($where,'id');
        if($user_info){
            $res = CardThumbService::S()->delThumb($where);
            if($res){
                $this->ajax(200,'取消点赞成功',$res);
            }else{
                $this->ajax(202,'取消点赞失败');
            }
        }else{
            $res = CardThumbService::S()->addThumb($save_data);
            if($res){
                $this->ajax(200,'点赞成功',$res);
            }else{
                $this->ajax(202,'点赞失败');
            }
        }


    }

}